﻿
@{
    Layout = null;
}


<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">企业属性</label>
            <div class="layui-input-inline">
                <select id="vendorSelect" name="enterpriseAttribute">
                    <option value="0">请选择企业属性</option>
                </select>
            </div>
        </div>
       </div>
        <div class="layui-form-item">
            <label class="layui-form-label">供应商编码</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="supplierCode" name="supplierCode" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input"  placeholder="请输入供应商编码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">中文全称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="name" name="name" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入中文全称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="address" name="address" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入地址">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">主联系人</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="mainContact" name="mainContact" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入主联系人">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="telephone" name="telephone" lay-verify="required" autocomplete="off" lay-affix="clear" class="layui-input" placeholder="请输入电话">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="add">确定</button>
                <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkStation()">关闭</button>
            </div>
        </div>
</form>

    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
    <script>

        //关闭弹框
        function closeWorkStation() {
            var index = parent.layer.getFrameIndex(window.name);//先得到当前的索引
            parent.layer.close(index);//再执行关闭
        }
layui.use(['form', 'laydate', 'util'], function(){
  var form = layui.form;
  var layer = layui.layer;
  var laydate = layui.laydate;
  var util = layui.util;

  // 提交事件
  form.on('submit(add)', function(data){
    var field = data.field; // 获取表单字段值
    // 显示填写结果，仅作演示用
    //layer.alert(JSON.stringify(field), {
    //  title: '当前填写的字段值'
    //});
    // 此处可执行 Ajax 等操作
      $.ajax({
          type: 'post',
          url: '/Supplier/InsertSupplier',
          dataType: 'json',
          data: field,
          success: function (res) {
              if (res > 0) {
                  layer.msg('添加成功')
                  closeWorkStation()
                  location.href = '/Supplier/GetSupplierList'
                  return false

              }
              if (res == -1) {
                  layer.msg('供应商编码已存在')
                  return false
              }
          }
      })
    return false; // 阻止默认 form 跳转
  });


    //获取供应商信息
    $(document).ready(function () {

        // 使用 AJAX 获取供应商数据  
        $.ajax({
            url: '/Supplier/GetDictItems?dictTypeId=' + 2, // 替换为你的 API 路径  
            type: 'get',
            dataType: 'json',
            async: true,
            success: function (res) {
                // 清空现有选项  
                $('#vendorSelect').empty();
                $('#vendorSelect').append('<option value="0">请选择供应商</option>');
                console.log(res)
                // 遍历返回的数据  
                $.each(res, function (index, vendor) {
                    $('#vendorSelect').append(
                        '<option value="' + vendor.dictItemName + '">' + vendor.dictItemName + '</option>');
                });
                // 重新渲染 layui 组件
                layui.form.render('select');
            },
            error: function (xhr, status, error) {
                console.error('获取供应商失败:', error);
            }
        });

        // 监听下拉框变化  
        $('#vendorSelect').change(function () {
            var selectedValue = $(this).val();
            console.log("选择的供应商ID:", selectedValue);
            // 根据选择的供应商进行其他操作  
        });
    });  

});
    </script>


